package com.cc.data_explore.gui.base;

import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import org.dromara.hutool.core.math.NumberUtil;

/**
 * 分页控件
 *
 * @author wenli
 * @date 2023/06/03
 */
public class PagingControl extends HBox {

    private Pagination pagination;
    private ComboBox<Integer> pageComboBox;
    private TextField toPageText;

    private final IntegerProperty total = new SimpleIntegerProperty(0);
    private final IntegerProperty pageSize = new SimpleIntegerProperty(1000);
    private final IntegerProperty pageNum = new SimpleIntegerProperty(1);
    private final ObservableList<Integer> pageItemList = FXCollections.observableArrayList(1000, 2000, 5000);

    public PagingControl() {
        initView();
        initListeners();
    }

    private void initView() {
        pagination = new Pagination(1);
        pagination.setStyle("-fx-page-information-visible:flase;");
        pagination.setMinHeight(50);
        pagination.setMaxHeight(50);
        Label totalLabel = new Label();
        pageComboBox = new ComboBox<>();
        toPageText = new TextField();
        toPageText.setPrefWidth(80);
        setMaxHeight(40);
        setMinHeight(40);
        setAlignment(Pos.CENTER_RIGHT);
        setSpacing(10);
        Label totalPageLabel = new Label();
        getChildren().addAll(new Label("共"), totalLabel, new Label("条"), pageComboBox, pagination, new Label("前往"), toPageText, totalPageLabel);
        pageComboBox.setCellFactory(call -> new ListCell<>() {
            @Override
            public void updateItem(Integer item, boolean empty) {
                super.updateItem(item, empty);
                if (item != null) {
                    setText(item + "条/页");
                } else {
                    setText(null);
                }
            }
        });
        totalLabel.textProperty().bind(Bindings.createStringBinding(() -> getTotal() + "", totalProperty()));
        totalPageLabel.textProperty()
                      .bind(Bindings.createStringBinding(() -> "页 共" + pagination.getPageCount() + "页", pagination.pageCountProperty()));
        pageComboBox.setItems(pageItemList);
        pageComboBox.getSelectionModel().select(0);
    }

    private void initListeners() {
        pageNum.bind(pagination.currentPageIndexProperty());
        pageSize.bind(pageComboBox.getSelectionModel().selectedItemProperty());
        toPageText.setOnKeyPressed(event -> {
            if (event.getCode().equals(KeyCode.ENTER)) {
                int index = NumberUtil.parseInt(toPageText.getText());
                if (index <= 0) {
                    index = 1;
                } else if (index > pagination.getPageCount()) {
                    index = pagination.getPageCount();
                }
                pagination.setCurrentPageIndex(index - 1);
            }
        });

        total.addListener((observable, oldValue, newValue) -> {
            // 总条数
            int totalCount = newValue.intValue();
            // 每页条数
            int pageSize = (getPageSize() == 0 ? 1 : getPageSize());
            // 计算总页数
            int totalPages = totalCount / pageSize;
            if (totalCount % pageSize != 0) {
                // 如果有余数，则总页数加1
                totalPages++;
            }
            if (totalCount == 0) {
                // 如果有余数，则总页数加1
                totalPages++;
            }
            pagination.setPageCount(totalPages);
        });

        pageComboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            // 总条数
            int totalCount = getTotal();
            // 每页条数
            int pageSize = (newValue == 0 ? 1 : newValue);
            // 计算总页数
            int totalPages = totalCount / pageSize;
            if (totalCount % pageSize != 0) {
                // 如果有余数，则总页数加1
                totalPages++;
            }
            pagination.setPageCount(totalPages);
        });

    }

    public void setPageSize(Integer pageSize) {
        pageComboBox.getSelectionModel().select(pageSize);
    }

    public ObservableList<Integer> getPageItemList() {
        return pageItemList;
    }


    public int getTotal() {
        return total.get();
    }

    public IntegerProperty totalProperty() {
        return total;
    }

    public void setTotal(int total) {
        this.total.set(total);
    }

    public int getPageSize() {
        return pageSize.get();
    }

    public IntegerProperty pageSizeProperty() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize.set(pageSize);
    }

    public int getPageNum() {
        return pageNum.get();
    }

    public IntegerProperty pageNumProperty() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum.set(pageNum);
    }
}
